package thread;

import java.util.concurrent.locks.ReentrantLock;

//ReentrantLock接口
public class Demo40 {
    private static int count;
    public static void main(String[] args) {
        ReentrantLock locker = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                locker.lock();
                try {
                    count++;
                } finally {
                    locker.unlock();
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                locker.lock();
                try {
                    count++;
                } finally {
                    locker.unlock();
                }
            }
        });
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }
}
